import { treeToList } from './tree'
/**
 * 递归遍历用户列表和部门列表， 返回指定部门下的所有子部门员工，而不是当前部门下的员工
 * @param userList
 * @param deptList
 * @param deptId
 */
export const userSelector = (userList, deptList, deptId) => {
  userList = JSON.parse(JSON.stringify(userList))
  deptList = JSON.parse(JSON.stringify(deptList))
  if (!deptList || deptList.length == 0) {
    return []
  }
  if (!userList || userList.length == 0) {
    return []
  }
  // 先flat 一下
  const list = treeToList(deptList)
  // 递归： ---
  let hasMore = true
  const findList = [deptId]
  const parentId = [deptId]
  while (hasMore) {
    hasMore = false
    for (let i = 0; i < list.length; i++) {
      if (parentId.includes(list[i].parentId)) {
        hasMore = true
        findList.push(list[i].id)
        parentId.push(list[i].id)
        list.splice(i, 1)
      }
    }
  }
  const returnList = userList.filter((user) => findList.includes(user.deptId))
  return returnList
}
